package com.yami.shop.api.controller;

import com.yami.shop.bean.model.LuckProduct;
import com.yami.shop.bean.model.LuckUser;
import com.yami.shop.bean.model.LuckUserProduct;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.wheelutil.RandomUtil;
import com.yami.shop.common.wheelutil.WeightMeta;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.LuckProductService;
import com.yami.shop.service.LuckUserProductService;
import com.yami.shop.service.LuckUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wheel")
public class LuckWheelController {

    @Autowired
    private LuckProductService luckProductService;

    @Autowired
    private LuckUserProductService luckUserProductService;

    @Autowired
    private LuckUserService luckUserService;

    /**
     * 授权存储用户信息
     * @return
     */
    @PostMapping("/wheelUser")
    public ResponseEntity<Boolean> wheelUser(String shopId){
        String uid = SecurityUtils.getUser().getUserId();
        //查询该用户所有的抽奖次数,判断用户有没有超过最大抽奖次数
        int userLuckTime = luckUserProductService.getAllLuckTime(uid,Long.valueOf(shopId));
        //获取设置的中奖次数
        LuckUser luckUser = luckUserService.getLuckData(Long.valueOf(shopId));
        int luckNumber = 0;
        int dayLuckNumber = 0;
        int winningTime = 0;
        int dayWinningTime = 0;
        if(luckUser!=null){
            luckNumber = luckUser.getLuckNumber();
            dayLuckNumber = luckUser.getDayLuckNumber();
            winningTime = luckUser.getWinningTime();
            dayWinningTime = luckUser.getDayWinningTime();
        }
        if(userLuckTime>=luckNumber){
            return ResponseEntity.ok(false);
        }
        //获取该用户今日的抽奖次数
        int currentDayLuckTime =  luckUserProductService.getcurrentDayLuckTime(uid,Long.valueOf(shopId));
        if(currentDayLuckTime>=dayLuckNumber){
            return ResponseEntity.ok(false);
        }
        //获取用户总的中奖次数
        int allWinningTime =  luckUserProductService.getAllWinningTime(uid,Long.valueOf(shopId));
        if(allWinningTime>winningTime){
            return ResponseEntity.ok(false);
        }
        //获取当前用户今天的中奖次数
        int userdayWinningTime = luckUserProductService.getdayWinningTime(uid,Long.valueOf(shopId));
        if(userdayWinningTime>dayWinningTime){
            return ResponseEntity.ok(false);
        }
        return ResponseEntity.ok(true);
    }

    /**
     * 查询产品信息
     */
    @GetMapping("/productList")
    public ResponseEntity<List<LuckProduct>> productList(String shopId) {
        LuckProduct luckProduct = new LuckProduct();
        luckProduct.setShopId(Long.valueOf(shopId));
        List<LuckProduct> productList = luckProductService.queryList(luckProduct);
        return ResponseEntity.ok(productList);
    }

    /**
     * 抽奖
     * @param
     * @return
     */
    @PostMapping("/lottery")
    public ResponseEntity<Map<String,Object>> wheelLottery(String shopId){
        Map map = new HashMap();
        String uid = SecurityUtils.getUser().getUserId();
        boolean flag=canDog(uid,shopId);
        if(!flag){
            map.put("code",0);
            map.put("message","没有可抽奖次数");
            return ResponseEntity.ok(map);
        }
        String  dod = luckDog(shopId);
        LuckUserProduct luckUserProduct = new LuckUserProduct();
        luckUserProduct.setUid(uid);
        luckUserProduct.setPname(dod);
        luckUserProduct.setShopId(Long.valueOf(shopId));
        luckUserProductService.save(luckUserProduct);
        map.put("code",1);
        map.put("message",dod);
        return ResponseEntity.ok(map);
    }

    private String luckDog(String shopId){
        LuckProduct luckProduct = new LuckProduct();
        luckProduct.setFlag(1);
        luckProduct.setShopId(Long.valueOf(shopId));
        List<LuckProduct> luckProducts = luckProductService.queryList(luckProduct);
        Map<String,Integer> tt=new HashMap<>();
        String dod = null;
        if(luckProducts!=null&&luckProducts.size()>0){
           for(int i=0;i<luckProducts.size();i++){
               LuckProduct luckProduct1=luckProducts.get(i);
               //查询当前奖品有没有超过最大中奖次数,超过了，就不再放入奖池中
               int winningTime = luckUserProductService.getWinningTimes(luckProduct1);
               int time = luckProduct1.getWinningTime();
               if(time==0){
                   tt.put(luckProduct1.getName(),luckProduct1.getWeight().intValue());
               }else if(time ==winningTime){

               }else{
                   tt.put(luckProduct1.getName(),luckProduct1.getWeight().intValue());
               }
           }
            WeightMeta<String> md = RandomUtil.buildWeightMeta(tt);
            dod = md.random();
        }
        return  dod;
    }


    //可以计算可以抽奖
    @PostMapping("/test")
    public boolean canDog(String uid,String shopId){
        //查询该用户所有的抽奖次数,判断用户有没有超过最大抽奖次数
        int userLuckTime = luckUserProductService.getAllLuckTime(uid,Long.valueOf(shopId));
        //获取设置的中奖次数
        LuckUser luckUser = luckUserService.getLuckData(Long.valueOf(shopId));
        int luckNumber = 0;
        int dayLuckNumber = 0;
        int winningTime = 0;
        int dayWinningTime = 0;
        if(luckUser!=null){
             luckNumber = luckUser.getLuckNumber();
             dayLuckNumber = luckUser.getDayLuckNumber();
             winningTime = luckUser.getWinningTime();
             dayWinningTime = luckUser.getDayWinningTime();
        }
        if(userLuckTime>=luckNumber){
            return false;
        }
        //获取该用户今日的抽奖次数
        int currentDayLuckTime =  luckUserProductService.getcurrentDayLuckTime(uid,Long.valueOf(shopId));
        if(currentDayLuckTime>=dayLuckNumber){
            return false;
        }
        //获取用户总的中奖次数
        int allWinningTime =  luckUserProductService.getAllWinningTime(uid,Long.valueOf(shopId));
        if(allWinningTime>winningTime){
            return false;
        }
        //获取当前用户今天的中奖次数
        int userdayWinningTime = luckUserProductService.getdayWinningTime(uid,Long.valueOf(shopId));
        if(userdayWinningTime>dayWinningTime){
            return false;
        }
        return true;
    }


}
